home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / smalltlk.zip / SOURCES / FILE.C < prev    next >
Text File  |  1990-09-12  |  3KB  |  156 lines

  1. /*
  2.     Little Smalltalk
  3.  
  4.         programs used by class File
  5.         timothy a. budd 11/84
  6. */
  7. /*
  8.     The source code for the Little Smalltalk System may be freely
  9.     copied provided that the source of all files is acknowledged
  10.     and that this condition is copied with each file.
  11.  
  12.     The Little Smalltalk System is distributed without responsibility
  13.     for the performance of the program and without any guarantee of
  14.     maintenance.
  15.  
  16.     All questions concerning Little Smalltalk should be addressed to:
  17.  
  18.         Professor Tim Budd
  19.         Department of Computer Science
  20.         Oregon State University
  21.         Corvallis, Oregon
  22.         97331
  23.         USA
  24. */
  25. # include <stdio.h>
  26. # include "object.h"
  27. # include "file.h"
  28. # include "string.h"
  29. # include "number.h"
  30. # include "primitiv.h"
  31.  
  32. static mstruct *fr_file = 0;    /* free file list */
  33.  
  34. object *new_file()
  35. {    struct file_struct *new;
  36.  
  37.     if (fr_file) {
  38.         new = (struct file_struct *) fr_file;
  39.         fr_file = fr_file->mlink;
  40.         }
  41.     else {
  42.         new = structalloc(struct file_struct);
  43.         }
  44.  
  45.     new->l_size = FILESIZE;
  46.     new->l_ref_count = 0;
  47.     new->file_mode = STRMODE;
  48.     new->fp = NULL;
  49.     return((object *) new);
  50. }
  51.  
  52. free_file(phil)
  53. struct file_struct *phil;
  54. {
  55.     if (! is_file(phil))
  56.         cant_happen(8);
  57.     if (phil->fp != NULL)
  58.         fclose(phil->fp);
  59.     ((mstruct *) phil)->mlink = fr_file;
  60.     fr_file = (mstruct *) phil;
  61. }
  62.  
  63. file_err(message)
  64. char *message;
  65. {    object *errp;
  66.     char buffer[150];
  67.  
  68.     sprintf(buffer,"File: %s", message);
  69.     sassign(errp, new_str(buffer));
  70.     primitive(ERRPRINT, 1, &errp);
  71.     obj_dec(errp);
  72. }
  73.  
  74. file_open(phil, name, type)
  75. struct file_struct *phil;
  76. char *name, *type;
  77. {    char buffer[100];
  78.  
  79.     if (phil->fp != NULL)
  80.         fclose(phil->fp);
  81.     phil->fp = fopen(name, type);
  82.     if (phil->fp == NULL) {
  83.         sprintf(buffer,"can't open: %s\n", name);
  84.         file_err(buffer);
  85.         }
  86. }
  87.  
  88. # define BUFLENGTH 250
  89.  
  90. object *file_read(phil)
  91. struct file_struct *phil;
  92. {    object *new;
  93.     int c;
  94.     char buffer[BUFLENGTH], *p;
  95.  
  96.     if (phil->fp == NULL) {
  97.         file_err("attempt to read from unopened file");
  98.         return(o_nil);
  99.         }
  100.     switch(phil->file_mode) {
  101.         case CHARMODE:
  102.             if (EOF == (c = fgetc(phil->fp)))
  103.                 new = o_nil;
  104.             else
  105.                 new = new_char(c);
  106.             break;
  107.         case STRMODE:
  108.             if (NULL == fgets(buffer, BUFLENGTH, phil->fp))
  109.                 new = o_nil;
  110.             else {
  111.                 p = &buffer[strlen(buffer) - 1];
  112.                 if (*p == '\n') *p = '\0';
  113.                 new = new_str(buffer);
  114.                 }
  115.             break;
  116.         case INTMODE:
  117.             if (EOF == (c = getw(phil->fp)))
  118.                 new = o_nil;
  119.             else
  120.                 new = new_int(c);
  121.             break;
  122.         default:
  123.             file_err("unknown mode");
  124.             new = o_nil;
  125.         }
  126.     return(new);
  127. }
  128.  
  129. file_write(phil, obj)
  130. struct file_struct *phil;
  131. object *obj;
  132. {
  133.     if (phil->fp == NULL) {
  134.         file_err("attempt to write to unopened file");
  135.         return;
  136.         }
  137.     switch(phil->file_mode) {
  138.         case CHARMODE:
  139.             if (! is_character(obj)) goto modeerr;
  140.             fputc(int_value(obj), phil->fp);
  141.             break;
  142.         case STRMODE:
  143.             if (! is_string(obj)) goto modeerr;
  144.             fputs(string_value(obj), phil->fp);
  145.             fputc('\n', phil->fp);
  146.             break;
  147.         case INTMODE:
  148.             if (! is_integer(obj)) goto modeerr;
  149.             putw(int_value(obj), phil->fp);
  150.             break;
  151.         }
  152.     return;
  153. modeerr:
  154.     file_err("attempt to write object of wrong type for mode");
  155. }
  156.